using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Baula.TextGen.Framework.Tests
{
    [TestClass]
    public class UpdateTests
    {
        private string PreviouslyGeneratedText { get; set; }

        [TestInitialize]
        public void InitializeTests()
        {
            const string templateContent =
                @"This is the main part
with its second
and its last line
' %Section:Preamble%
' === PartA
%PCnt%: This is some %Info% info from PartA
' @Section:Preamble
%PCnt%: This %PInfo% goes into the preamble";
            const string expectedOutput = @"This is the main part
with its second
and its last line
Part 1: This cool preamble info goes into the preamble
Part 2: This useless preamble info goes into the preamble
' %Section:Preamble%
Part 1: This is some very important info from PartA
Part 2: This is some totally unimportant info from PartA";
            var documentTemplate = new DocumentTemplate(templateContent);
            var partATemplate = documentTemplate.PartTemplates["PartA"];
            var document = documentTemplate.CreateDocument();
            var partA1 = partATemplate.CreatePart();
            partA1.ValuesForPlaceholders["PCnt"] = "Part 1";
            partA1.ValuesForPlaceholders["Info"] = "very important";
            partA1.ValuesForPlaceholders["PInfo"] = "cool preamble info";
            document.Parts.Add(partA1);
            var partA2 = partATemplate.CreatePart();
            partA2.ValuesForPlaceholders["PCnt"] = "Part 2";
            partA2.ValuesForPlaceholders["Info"] = "totally unimportant";
            partA2.ValuesForPlaceholders["PInfo"] = "useless preamble info";
            document.Parts.Add(partA2);

            PreviouslyGeneratedText = document.RenderToString(RenderMode.Updatable);
            Console.WriteLine(PreviouslyGeneratedText);

            Assert.AreEqual(expectedOutput, PreviouslyGeneratedText);
        }

        [TestMethod]
        public void PartTemplateAddedToAlreadyGeneratedText_WhenPartIsAdded_ThatPartsGetsAppendedToTheOriginalContent()
        {
            const string partTemplateContent =
                @"This is something %HowIsIt%
that can be added afterwards.
' @Section:Preamble
It is even possible to append something to the preamble";
            const string expectedOutput = @"This is the main part
with its second
and its last line
Part 1: This cool preamble info goes into the preamble
Part 2: This useless preamble info goes into the preamble
It is even possible to append something to the preamble
' %Section:Preamble%
Part 1: This is some very important info from PartA
Part 2: This is some totally unimportant info from PartA
This is something great to have
that can be added afterwards.";

            var documentTemplate = new DocumentTemplate(PreviouslyGeneratedText);
            var additionalPartTemplate = PartTemplate.CreateFromContent(partTemplateContent);
            var part = additionalPartTemplate.CreatePart();
            part.ValuesForPlaceholders["HowIsIt"] = "great to have";

            var document = documentTemplate.CreateDocument();
            document.Parts.Add(part);

            var actualContent = document.RenderToString(RenderMode.Updatable);
            
            Assert.AreEqual(expectedOutput, actualContent);
        }
    }
}